package com.bangbang.controller;


import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.feign.PayServiceFeign;
import com.bangbang.mapper.TbGroupCourseOrderMapper;
import com.bangbang.mapper.TbMemberMapper;
import com.bangbang.mapper.TbStudentUserMapper;
import com.bangbang.pojo.TbGroupCourseOrder;
import com.bangbang.pojo.TbMember;
import com.bangbang.pojo.TbStudentUser;
import com.bangbang.util.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-08-12
 */
@RestController
@RequestMapping("tbMember")
public class TbMemberController {

    @Autowired
    private PayServiceFeign payServiceFeign;

    @Autowired
    private TbMemberMapper tbMemberMapper;


    @Autowired
    private TbStudentUserMapper tbStudentUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TbGroupCourseOrderMapper tbGroupCourseOrderMapper;

//    会员充值
    @ApiOperation(value = "会员增加方法") //  接口文档方法名
    @ApiImplicitParams({            //  接口文档  解释参数
            @ApiImplicitParam(name = "price",value = "会员价格",required = true,paramType = "query")
    })
    @GetMapping  //  post 请求 新增 复杂查询等方法
    public Result payMember(@RequestParam("price") BigDecimal price){
        IdWorker idWorker = new IdWorker();
        //创建订单id
        long orderNumber = idWorker.nextId();
//        生成订单
        TbGroupCourseOrder tbGroupCourseOrder = new TbGroupCourseOrder();
        Object o = redisTemplate.boundValueOps("8ac5cfcb-4bc3-4050-aef5-f0ef18e0128c").get();
        String s = o.toString();
        Long aLong = Long.valueOf(s);
        tbGroupCourseOrder.setGcoGroId(orderNumber).setGcoGroPrice(price).setGcoUserId(aLong).setGcoCreateTime(new Date());
        tbGroupCourseOrderMapper.insert(tbGroupCourseOrder);
        //  会员充值
        String subject = "会员充值";
        String body = "帮帮课堂会员";
        String result = payServiceFeign.alipayTradePagePay(orderNumber,price,subject,body);
        System.out.println(result);
        return Result.buildResult(ResultEnum.SUCCESS).setResult(result);
    }


    // 增,修改订单
//    @ApiOperation(value = "会员增加方法,并修改用户信息") //  接口文档方法名
//    @ApiImplicitParams({            //  接口文档  解释参数
//            @ApiImplicitParam(name = "orderId",value = "订单id",required = true,paramType = "query"),
//            @ApiImplicitParam(name = "bigMoney",value = "价格",required = true,paramType = "query"),
//            @ApiImplicitParam(name = "trade_no",value = "流水号",required = true,paramType = "query")
//    })
//    @PostMapping         //  post 请求 新增 复杂查询等方法
//    public Result save(@RequestParam String orderId, @RequestParam String bigMoney,@RequestParam String trade_no){
//        //  增加
//        // 获取用户信息,用户id模拟已经拿到
//        IdWorker idWorker = new IdWorker();
//        //创建订单id
//        long orderNumber = idWorker.nextId();
////        Result<MemberDTO> result = memberServiceFeign.alipayTradePagePay(orderNumber,price,subject,body);
//            //MemberDTO memberDTO = result.getEntity();
//           // String orderId = memberDTO.getOut_trade_no();// 订单号
//           // String bigMoney = memberDTO.getTotal_amount();// 金额
//            BigDecimal money = new BigDecimal(bigMoney);
//            money = money.setScale(2, BigDecimal.ROUND_HALF_UP);
//            String waterId = trade_no;
//            //String waterId = memberDTO.getTrade_no();//   流水号
////              模拟数据
//         Long userId = Long.valueOf(11111);  // 模拟当前用户id
////        String orderId = "123";  // 订单号
////        BigDecimal money = new BigDecimal(12.1); // 金额
////        money = money.setScale(2, BigDecimal.ROUND_HALF_UP);
////        String waterId = "123";  // 流水号
//
////
//        TbMember tbMember = new TbMember();
//        Date date = new Date();
//        Calendar rightNow = Calendar.getInstance();
//        rightNow.setTime(date);
//        rightNow.add(Calendar.MONTH,1);
//        Date time = rightNow.getTime();   //      30天的会员
//        long d = (time.getTime() - date.getTime());
//        double day = Math.floor((d / (24 * 60 * 60 * 1000)));
//        int days = (int) day;
//        String Residuetime = String.valueOf(days);
//        long o = 12;
//        tbMember.setMemberId(o)
//                .setMemberOrderId(orderId)
//                .setMemberUserId(userId)
//                .setMemberWaterId(waterId)
//                .setMemberMoney(money)
//                .setMemberCreatetime(date)
//                .setMemberResiduetime(Residuetime)
//                .setMemberOvertime(time);   // 计算 开始和剩余之间的差
//        tbMemberMapper.insert(tbMember);
//        //查出来当前用户信息,并且修改信息
//        Map<String, Object> map = new HashMap<>();
//        QueryWrapper<TbStudentUser> studentUserQueryWrapper = new QueryWrapper<>();
//        studentUserQueryWrapper.eq("user_id", userId);   // 用户id是模拟的
//        TbStudentUser tbStudentUser = new TbStudentUser();
//        tbStudentUser.setUserMemberCreateTime(date).setUserMemberOverTime(time).setUserWhetherMember(1);
//        tbStudentUserMapper.update(tbStudentUser,studentUserQueryWrapper);
//
//
//        return Result.buildResult(ResultEnum.SUCCESS);
//    }


}

